package club.dataresources.service.impl;

import club.dataresources.entity.Customer;
import club.dataresources.entity.CustomerExample;
import club.dataresources.mapper.CustomerMapper;
import club.dataresources.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author
 * @create 2018-10-30 20:38
 **/
@Service
public class LoginServiceImpl implements LoginService{

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Customer register(Customer customer) throws Exception {
        //查重添加
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(customer.getName());
        List<Customer> liName = customerMapper.selectByExample(example);
        if(liName!=null && liName.size()>0){
            return null;
        }else{
            customerMapper.insertSelective(customer);
            return customer;
        }
    }

    @Override
    public Customer login(Customer customer) throws Exception {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(customer.getName());
        List<Customer> liName = customerMapper.selectByExample(example);

        if(liName!=null && liName.size()==1){
            return liName.get(0);
        }else{
            return null;
        }
    }
}
